package org.example.ch10.config;

import org.springframework.context.annotation.Configuration;
import org.springframework.messaging.simp.config.MessageBrokerRegistry;
import org.springframework.web.socket.config.annotation.EnableWebSocketMessageBroker;
import org.springframework.web.socket.config.annotation.StompEndpointRegistry;
import org.springframework.web.socket.config.annotation.WebSocketMessageBrokerConfigurer;

/**
 * @ClassName WebSocketConfig
 * @Description TODO
 * @Author Kx
 * @Date 2025/9/21 19:26
 * @Version 1.0
 */
@Configuration
// 启用 WebSocket 消息代理
@EnableWebSocketMessageBroker
public class WebSocketConfig implements WebSocketMessageBrokerConfigurer {

    /**
     * @author: kx
     * @date: 2025/9/21 19:30
     * @param registry : Broker 注册器
     * @description: 配置消息代理，用户订阅者和发布者使用，通过传入的 Broker 注册器来注册一个 Broker
    */
    @Override
    public void configureMessageBroker(MessageBrokerRegistry registry) {
        // 创建 spring 内置的简单消息队列并设置一个主题前缀（可以同时设置多个），用于消息的订阅和发布
        registry.enableSimpleBroker("/news", "/sports");
    }

    /**
     * @author: kx
     * @date: 2025/9/21 19:32
     * @param registry : 注册器
     * @description: 注册 STOMP 连接端点
    */
    @Override
    public void registerStompEndpoints(StompEndpointRegistry registry) {
        registry.addEndpoint("message-broker");
    }
}
